home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright (C) 1994, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
- #include "sw.h"
- #include "extern.h"
- #include "stars.h"
- #include "main.h"
- #include <Inventor/actions/SoGLRenderAction.h>
- #include <Inventor/nodes/SoSeparator.h>
- #include <Inventor/nodes/SoTranslation.h>
- #include <Inventor/nodes/SoCoordinate3.h>
- #include <Inventor/nodes/SoPointSet.h>
- #include <Inventor/nodes/SoLightModel.h>
- #include <Inventor/nodes/SoMaterialBinding.h>
- #include <Inventor/nodes/SoMaterial.h>
- #include <Inventor/nodes/SoComplexity.h>
- #include <Inventor/nodes/SoSphere.h>
- #include <Inventor/nodes/SoCallback.h>
- #include <gl/gl.h>
- #include <gl/glws.h>
-
- #define STARRADIUS (100.0)
- #define SUNRADIUS (STARRADIUS / 20.0)
- #define SUNSIDES 12
-
- static float starColors[][3] = {
- {1.0, 1.0, 1.0},
- {1.0, 1.0, 0.5},
- {1.0, 0.5, 0.5},
- {1.0, 1.0, 1.0},
- {1.0, 1.0, 0.5},
- {0.5, 0.5, 1.0} };
- #define NUMSTARCOLORS (sizeof(starColors)/sizeof(float[3]))
- static float sunColor[3] = { 1.0, 1.0, 0.5 };
- static float (*starPositions)[3],
- (*sunVertices)[3];
- static int numStars,
- numStarsPerColor;
- static int starsFrameNumber = -1;
-
- // The stars must be drawn before the camera is translated so that they
- // stay a constant distance from the ship. Also they must be drawn
- // without modifying the depth buffer so they never appear above objects
- // in local space. This also means they must be drawn before local
- // objects.
-
- static void drawStars()
- {
- if (!isPaused() && frameNumber == starsFrameNumber) // avoid extra redraws
- return;
- starsFrameNumber = frameNumber;
- frameDrawn = TRUE;
-
- GLXwinset(display, view->getNormalWindow());
-
- zwritemask(0x0); // don't touch the z buffer
-
- // draw stars
- for (int i = 0, k = 0; i < NUMSTARCOLORS; i++) {
- c3f(starColors[i]); // new color
- bgnpoint();
- for (int j = 0; j < numStarsPerColor; k++, j++)
- v3f(starPositions[k]);
- endpoint();
- }
- if (k < numStars) { // finish off remaining stars
- bgnpoint();
- for (; k < numStars; k++)
- v3f(starPositions[k]);
- endpoint();
- }
-
- // draw sun
- c3f(sunColor);
- for (i = 1; i < SUNSIDES-1; i++) {
- bgnpolygon();
- v3f(sunVertices[0]);
- v3f(sunVertices[i+1]);
- v3f(sunVertices[i]);
- endpolygon();
- }
-
- zwritemask(0xffffffff); // allow z buffer writes
- }
-
- static void starsAction(void*, SoAction* action)
- {
- if (action->getTypeId() == SoGLRenderAction::getClassTypeId())
- drawStars();
- }
-
- SoNode* makeStars(int num)
- {
- SoSeparator* starRoot = new SoSeparator; // all stars under here
-
- sunVertices = (float(*)[3])new float[SUNSIDES*3];
- for (int i = 0; i < SUNSIDES; i++) {
- sunVertices[i][0] = STARRADIUS;
- sunVertices[i][1] = SUNRADIUS * cos(2.0 * M_PI * float(i) / SUNSIDES);
- sunVertices[i][2] = SUNRADIUS * sin(2.0 * M_PI * float(i) / SUNSIDES);
- }
-
- // position stars on a sphere
- if (num < 0) num = 0;
- numStars = num;
- numStarsPerColor = numStars / NUMSTARCOLORS;
- starPositions = (float(*)[3])new float[numStars*3];
- for (i = 0; i < numStars; i++) {
- float x = drand48()-0.5, y = drand48()-0.5, z = drand48()-0.5;
- float d = sqrt(x*x + y*y + z*z);
- if (d < 1e-5 || d > 1.0) i--;
- else {
- starPositions[i][0] = STARRADIUS * x / d;
- starPositions[i][1] = STARRADIUS * y / d;
- starPositions[i][2] = STARRADIUS * z / d;
- }
- }
-
- // make star callback node
- SoCallback* starsCallback = new SoCallback;
- starsCallback->setCallback(starsAction);
-
- return starsCallback;
- }
-